<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/xing.jpeg" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        class="login_form"
        :model="login_Form"
        :rules="loginFormRules"
        ref="loginFormRef111"
      >
        <!--登录名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
            v-model="login_Form.username"
          ></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            placeholder="请输入密码"
            v-model="login_Form.password"
          ></el-input>
        </el-form-item>
        <!-- 点击按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      login_Form: {
        username: "admin",
        password: 123456,
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入登录名称",
            trigger: "blur",
          },
          {
            min: 5,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      //重置功能
      //resetFields 内置重置的方法
      this.$refs.loginFormRef111.resetFields();
    },
    //登录功能
    login() {
      this.$refs.loginFormRef111.validate(async (valide) => {
        //resetFields 内置验证的回调函数
        if (!valide) return console.log("登录失败!");
        //验证成功的话
        const { data: res } = await this.$http.post("login", this.login_Form); //{data : res} 对象解构
        if (res.meta.status !== 200) return this.$message.error("登录失败!");
        this.$message.success("登录成功!");
        //保存token
        window.sessionStorage.setItem("token", res.data.token);
        // 导航至/home
        this.$router.push("/Home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 400px;
  height: 260px;
  background-color: #eee;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  flex: 1;
  text-align: right;
}
</style>